<?php
/**
 * <i>@fetch</i>组件
 * 
 * 定义新的模板变量
 * 
 * 可以使用的参数有：
 * - x-items    mixed 数组或对象
 * - [x-key] string 键值
 * 
 * 实例:
 * 
 *  模板：
 *  <code>
 *   <span com="@fetch" x-items="$developers" x-key="1"/>
 *  </code>
 *
 * @version $Id: IMintFetchComponent.php 312 2008-04-23 16:29:38Z iwind $
 * @package util.mint.component
 * @since 0.1
 */
class IMintFetchComponent extends IAbstractMintComponent {
	/**
	 * 取得支持的参数列表
	 *
	 * @return array
	 * @since 0.1
	 */	
	public function getSupportedParameters() {
		return array("x-key", "x-items");
	}
	
	/**
	 * 取得HTML属性映射
	 *
	 * @return array
	 * @since 0.1
	 */		
	public function getHtmlAttrMapping() {
		return array();
	}	
	
	/**
	 * 处理组件
	 *
	 * @return boolean
	 * @since 0.1
	 */	
	public function processComponent() {
		//parse items
		$xItems = $this->getParameterValue("x-items");
		if (is_null($xItems)) {
			$this->throwUndefinedValueException("x-items");
		}
		$xItemVar = new IMintVariable($xItems);
		if (!$xItemVar->isValid()) {
			$this->throwVariableException("x-items");
		}
		$process = $xItemVar->getParseProcess();
		$xItemValue = $xItemVar->getVarname();
		
		//parse x-key
		$xKey = $this->getParameterValue("x-key");
		if (is_null($xKey)) {
			$this->throwUndefinedValueException("x-key");
		}
		$xKeyVar = IMintVariable::getDefinition($xKey);
		if ($xKeyVar == null) {
			$xKeyVar = "\"" . addslashes($xKey) . "\"";
		}
		
		$php = "<?php\n{$process}\n";
		$php .= "echo IReflection::getProperty({$xItemValue}, {$xKeyVar})";
		$php .= "?>";
		
		$this->addFirstChildScript($php);
		
		return true;
	}
}

?>